package com.huluobo.high3videoproject.fragment;

import android.content.Intent;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.blankj.utilcode.util.SPUtils;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.huluobo.base.view.BaseFragment;
import com.huluobo.base.view.Event;
import com.huluobo.high3videoproject.R;
import com.huluobo.high3videoproject.adapter.MineFragmentAdapter;
import com.huluobo.high3videoproject.bean.TabEntity;
import com.huluobo.high3videoproject.bean.UserBeanResult;
import com.huluobo.high3videoproject.bean.UserInfo;
import com.huluobo.high3videoproject.mvp.goods.view.FollowFragment;
import com.huluobo.high3videoproject.mvp.like.view.LikeFragment;
import com.huluobo.high3videoproject.net.Constant;
import com.huluobo.high3videoproject.setting.view.SettingActivity;
import com.huluobo.high3videoproject.utils.DBUtils;
import com.huluobo.high3videoproject.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

public class MineFragment extends BaseFragment {
    private ImageView mine_iv_user_icon, mine_iv_setting;

    private TextView mine_tv_nick_name, mine_tv_desc;
    private UserBeanResult userBeanResult;

    private CommonTabLayout mine_tabs;

    private ViewPager2 mine_viewPager;

    //viewPager2的adapter
    private MineFragmentAdapter fragmentAdapter;

    //tab的数据源
    private ArrayList<CustomTabEntity> tabEntities = new ArrayList<>();

    //fragment的数据源
    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected int bindLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initData() {
        UserInfo userInfo = DBUtils.getInstance().queryUserInfo(SPUtils.getInstance().getString(Constant.SP_USERNAME));
        mine_tv_nick_name.setText(userInfo.getNickname());
        GlideUtils.load(getContext(), mine_iv_user_icon, userInfo.getIcon(), 0, 0, 20);
    }

    @Override
    protected void initView() {
        mine_iv_user_icon = (ImageView) findViewById(R.id.mine_iv_user_icon);
        mine_iv_setting = (ImageView) findViewById(R.id.mine_iv_setting);
        mine_tv_nick_name = (TextView) findViewById(R.id.mine_tv_nick_name);
        mine_tv_desc = (TextView) findViewById(R.id.mine_tv_desc);
        mine_tabs = (CommonTabLayout) findViewById(R.id.mine_tabs);
        mine_viewPager = (ViewPager2) findViewById(R.id.mine_viewPager);
        mine_iv_user_icon.setOnClickListener(v -> {
            jumpSettingPage();
        });
        mine_iv_setting.setOnClickListener(v -> {
            jumpSettingPage();
        });

        mine_tv_nick_name.setOnClickListener(v -> {
            jumpSettingPage();
        });

        //设置tab
        tabEntities.clear();
        tabEntities.add(new TabEntity("喜欢", 0, 0));
        tabEntities.add(new TabEntity("收藏", 0, 0));
        tabEntities.add(new TabEntity("关注", 0, 0));
        mine_tabs.setTabData(tabEntities);

        //设置tab的监听
        mine_tabs.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mine_viewPager.setCurrentItem(position);//设置tab和vp的联动
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        //设置vp相关的
        fragments.clear();
        fragments.add(new LikeFragment());
        fragments.add(new LikeFragment());
        fragments.add(new FollowFragment());

        fragmentAdapter = new MineFragmentAdapter(getActivity());
        fragmentAdapter.setFragments(fragments);
        mine_viewPager.setAdapter(fragmentAdapter);

        mine_viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mine_tabs.setCurrentTab(position);
            }
        });
    }

    private void jumpSettingPage() {
        //跳转到设置页面
        Intent intent = new Intent(getActivity(), SettingActivity.class);
        getActivity().startActivity(intent);
    }

    @Override
    protected void handEvent(Event event) {
        UserInfo userInfo = DBUtils.getInstance().queryUserInfo(SPUtils.getInstance().getString(Constant.SP_USERNAME));
        if (event.getType() == 103) {
            GlideUtils.loadNormal(getActivity(), mine_iv_user_icon, userInfo.getIcon());
        }
        if (event.getType() == 104) {
            mine_tv_nick_name.setText(userInfo.getNickname());
        }
    }
}
